package me.bincker.formwork.web.modules.system.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import me.bincker.formwork.web.constant.REG_PHONE
import me.bincker.formwork.web.core.entity.BaseEntity
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.FetchType
import javax.persistence.ManyToMany
import javax.validation.constraints.Email
import javax.validation.constraints.NotEmpty
import javax.validation.constraints.NotNull
import javax.validation.constraints.Pattern

@Entity
open class User: BaseEntity() {
    @Column(unique = true, nullable = false)
    @NotEmpty
    @Pattern(regexp = "^[a-zA-Z0-9_]{5,16}$", message = "用户名必须由五至十六位大小写字母、数字和下划线构成")
    var username: String? = null

    @JsonIgnore
    var encodedPassword: String? = null

    var nickname: String? = null

    @NotNull(message = "请填写性别")
    var gender: Gender? = null

    @Email
    var email: String? = null

    @NotEmpty(message = "请填写手机号")
    @Pattern(regexp = REG_PHONE, message = "请输入正确的手机号")
    var phoneNumber: String? = null

    var locked: Boolean? = null

    @NotEmpty(message = "请选择角色")
    @ManyToMany(fetch = FetchType.EAGER)
    var roleList: MutableSet<Role>? = null

    companion object{
        enum class Gender {
            //    男性
            MALE,
            //    女性
            FEMALE,
            //    保密
            SECURITY
        }
    }
}